package easier.app.x.starter;

import static androidx.core.app.NotificationCompat.EXTRA_CHANNEL_ID;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;

/**
 * @author Liujin 2018-11-08:14:36
 */
public class IntentBuilder {

    private IntentBuilder() {}

    /**
     * @return 设置界面
     */
    public static Intent settingIntent() {

        return new Intent( Settings.ACTION_SETTINGS );
    }

    /**
     * @return 所有应用设置界面
     */
    public static Intent appSettingsIntent() {

        return new Intent( Settings.ACTION_APPLICATION_SETTINGS );
    }

    /**
     * @param context context
     *
     * @return 应用设置界面
     */
    public static Intent appSettingIntent( Context context ) {

        Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS );
        intent.setData( Uri.fromParts( "package", context.getPackageName(), null ) );
        return intent;
    }

    /**
     * @param context context
     *
     * @return 应用详细设置
     */
    public static Intent appDetailSetting( Context context ) {

        Uri packageURI = Uri.fromParts( "package", context.getPackageName(), null );
        return new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI );
    }

    /**
     * @return gps设置界面
     */
    public static Intent GPSSetting() {
        //跳转GPS设置界面
        return new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
    }

    /**
     * @return 网络设置
     */
    public static Intent wirelessSetting() {

        return new Intent( Settings.ACTION_WIRELESS_SETTINGS );
    }

    /**
     * @return 无线网络设置
     */
    public static Intent wifiSetting() {

        return new Intent( Settings.ACTION_WIFI_SETTINGS );
    }

    /**
     * @return 数据漫游
     */
    public static Intent dataRoaming() {

        return new Intent( Settings.ACTION_DATA_ROAMING_SETTINGS );
    }

    /**
     * @return 飞行模式
     */
    public static Intent airPlaneModeSetting() {

        return new Intent( Settings.ACTION_AIRPLANE_MODE_SETTINGS );
    }

    /**
     * @return 蓝牙设置界面
     */
    public static Intent blueToothSetting() {

        return new Intent( Settings.ACTION_BLUETOOTH_SETTINGS );
    }

    /**
     * @param context 跳转到通知界面
     */
    public static Intent openAppNotification( Context context ) {

        try {
            // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
            Intent intent = new Intent();

            //这种方案适用于 API 26, 即8.0(含8.0)以上可以用
            if ( VERSION.SDK_INT >= VERSION_CODES.O ) {
                intent.setAction( Settings.ACTION_APP_NOTIFICATION_SETTINGS );
                intent.putExtra( Settings.EXTRA_APP_PACKAGE, context.getPackageName() );
                intent.putExtra( EXTRA_CHANNEL_ID, context.getApplicationInfo().uid );
            }

            //这种方案适用于 API21——25，即 5.0——7.1 之间的版本可以使用
            intent.putExtra( "app_package", context.getPackageName() );
            intent.putExtra( "app_uid", context.getApplicationInfo().uid );

            return intent;
        } catch ( Exception e ) {
            e.printStackTrace();

            // 出现异常则跳转到应用设置界面：
            // 下面这种方案是直接跳转到当前应用的设置界面。
            return IntentBuilder.appSettingIntent( context );
        }
    }
}
